<?php

class ManageController
{
    private static $controllerCache;
    private $adminuids;
    private $actions;
    private $diaryListCacheModel;
    private $topicModel;
    private $groupInfoModel;
    private $topicCommentsModel;

    function __construct()/*{{{*/
    {
    	$this->diaryListCacheModel    =    new DiaryListCacheModel();
    	$this->topicModel          	  =    new TopicModel();
    	$this->groupInfoModel         =    new GroupInfoModel();
    	$this->topicCommentsModel     =    new TopicCommentsModel();
    	
    	$this->adminuids =   array('3254365','3804421');
    	$uids = $this->diaryListCacheModel->getAdminUserList();
    	if(!empty($uids) && is_array($uids)){
    		$this->adminuids =   $uids;
    	}
    	$this->actions =   array('getAdminList','chainUser','unForbidDevice','removeDiary','stickTopic','removeTopic','delTopicComment','editGroup','shutUpUser',
    							'removeTopics','removeTopicComments','revertTopic','getUnpublishedTopics','promoteTopic'
    	);

    }/*}}}*/
    
    public function callApi($param)/*{{{*/
    {
    	$ac = $param['action'];
        # check $do /*{{{*/
        $doCheck    =   $this->checkAction($ac);
        if (!empty(AC::$errorNo))
        {
            return  $doCheck;
        } /*}}}*/
        return $this->$ac($param);
    }/*}}}*/
    
    private function getAdminList()/*{{{*/
    {
    	return $this->adminuids;
    }
    
    /**
     * 封禁用户
     * @param unknown $param
     * @return unknown
     */
    private function chainUser($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
        $controller =   $this->newController('UserController');
        $info       =   $controller->chainUser($param);
        
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][chainUser]'. DOP::encodeData($param). "[result='.$info.'] \n", TRUE);
        if($info){
        	AC::$userExpAction['action'] = "forbidUser"; //类型
        	AC::$userExpAction['admin'] = TRUE; //管理员操作标记
        	AC::$userExpAction['userid'] =  $param['uid']; //被操作用户
        	return TRUE;
        }
    	return  FALSE;
    }/*}}}*/    
    
    /**
     * 解封设备
     * @param  $param
     * @return 
     */
    private function unForbidDevice($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
        $controller =   $this->newController('UserController');
        $info  =   $controller->unForbidDevice($param);
        
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][unForbidDevice]'. DOP::encodeData($param). "[result='.$info.'] \n", TRUE);
        if($info){
        	return TRUE;
        }
    	return  FALSE;
    }/*}}}*/  
    
    /**
     * 封禁用户
     * @param unknown $param
     * @return unknown
     */
    private function addAdmin($param)/*{{{*/
    {
        if(!$this->checkAdminUid()){return  FALSE;}
         
        $result = $this->diaryListCacheModel->addAdminUser($param['uid']);
    
        UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][addAdmin]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
        
        return  $result;
    }/*}}}*/
    
    /**
     * 删除日记
     * @param $param[uid],id,bodyId,lable
     * @return boolean|unknown
     */
    public function removeDiary($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	$controller     =   $this->newController('DiaryController');
    	$info           =   $controller->removeDiary($param);
    	if($info){
    	    AC::$userExpAction['action'] = "deleteDiaryByadmin"; //类型
    	    AC::$userExpAction['admin'] = TRUE; //管理员操作标记
    	    AC::$userExpAction['userid'] =  $param['uid']; //被操作用户
    	}
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][RemoveDiary]'. DOP::encodeData($param).  "[result='.$info.'] \n", TRUE);
    	return  $info;
    }/*}}}*/
    
	/**
	 *  删除评论
	 */
    private function removeComment($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
    	$controller =   $this->newController('CommentController');
    	$info       =   $controller->removeComment($param);
    	if ($info){
    	    AC::$userExpAction['action'] = "deleteCommentByadmin"; //类型
    	    AC::$userExpAction['admin'] = TRUE; //管理员操作标记
    	    AC::$userExpAction['userid'] =  $param['uid']; //被操作用户
    	}
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeComment]'. DOP::encodeData($param). "[result='.$info.'] \n", TRUE);
    	return  $info;
    }/*}}}*/

    private function checkAdminUid()/*{{{*/
    {
    	$uid = AC::HOST_UID;
    	return in_array($uid, $this->adminuids);
    }/*}}}*/
    
    private function checkAction($action)/*{{{*/
    {
    	if (in_array($action, $this->actions) && method_exists($this, $action))  return  TRUE;
    	AC::$errorNo    =   'SA004';
    	return  ' Error : call do is not exists in ManageController! ';
    }/*}}}*/
    
    private function newController($controller)
    {
    	if (empty($controller))
    	{
    		AC::$errorNo    =   'AA003';
    		return  FALSE;
    	}
    	if (isset(self::$controllerCache[$controller])  &&  is_resource(self::$controllerCache[$controller]))
    	{
    		return  self::$controllerCache[$controller];
    	}
    	self::$controllerCache[$controller] =   new $controller();
    	return  self::$controllerCache[$controller];
    }/*}}}*/
    
    /**
     * 话题，置顶/加精
     * @param unknown $info
     * @return boolean
     */
    private function stickTopic($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
        $controller 	=   $this->newController('GroupController');
        $result         =   $controller->callApi($param);
        // 如果第一次置顶则增加经验
        if ($result['stick'] && $result['result']){
            $controller 	=   $this->newController('UserController');
            $info['uid']    =   $param['author_uid'];
            $info['exp']    =   20;
            $result         =   $controller->changeUserExp($info);
        }
        UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][stickTopic]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }/*}}}*/

    /**
     * 删除话题
     * @param unknown $param
     * @return boolean
     */
    private function removeTopic($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
    	if (empty($param['tid']) || empty($param['gid'])|| empty($param['author_uid']))
    	{
    		AC::$errorNo    =   'GRC001';
    		return  FALSE;
    	}
        $result 	= $this->topicModel->removeTopic($param);
        if (empty($result))
        {
            AC::$errorNo = 'GRC007';
            return  FALSE;
        }
        // 圈子话题数减少
        $this->groupInfoModel->delTopicNum($param);
        // 删除缓存中的话题信息
        $this->diaryListCacheModel->delTopicInfo($param['tid']);

        if ($result){
        	AC::$userExpAction['action'] = "deleteTopicByadmin"; //类型
        	AC::$userExpAction['admin'] = TRUE; //管理员操作标记
        	AC::$userExpAction['userid'] =  $param['author_uid']; //被操作用户
        }
        UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeTopic]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }
    // 删除某人所有的帖子、多选删除
    private function removeTopics($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	 
    	if ((empty($param['tid']) || empty($param['gid'])) && empty($param['uid']))
	    {
	        AC::$errorNo    =   'GRC001';
	        return  FALSE;
	    }
	        
	    // 获取用户所有帖子
	    if (isset($param['uid']) && intval($param['uid']) > 0)
	    {
	        $topics = $this->topicModel->getUserAllTopics(array('uid'=>$param['uid']));
	    }
	        
        foreach ($topics as $value)
        {
        	$topic 	= $this->topicModel->removeTopic(array('tid'=>$value['tid']));
        	$num 	= $this->groupInfoModel->delTopicNum(array('gid'=>$value['gid']));
        	// 删除缓存中的话题信息
        	$this->diaryListCacheModel->delTopicInfo(array('tid'=>$value['tid']));
        }
        if (empty($num))
        {
        	AC::$errorNo = 'GRC007';
        	return  FALSE;
        }
        UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeTopics]'. DOP::encodeData($param). "[result='.$num.'] \n", TRUE);
        return  TRUE;
    }
    
    // 还原某个用户在某个圈子发表的所有话题
    private function revertTopic($param)/*{{{*/
    {
        if(!$this->checkAdminUid()){return  FALSE;}
        
        if ((empty($param['gid'])) && empty($param['uid']))
        {
        	AC::$errorNo    =   'GRC001';
        	return  FALSE;
        }
    	$result 	= $this->topicModel->revertTopic($param);
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][revertTopic]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }
    
    // 删除某个用户的所有评论
    private function removeTopicComments($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
    	if ((empty($param['tid']) || empty($param['position']) || empty($param['author_uid'])) && empty($param['uid']))
    	{
    		AC::$errorNo    =   'GRC001';
    		return  FALSE;
    	}
    	
    	// 删除用户所有评论
    	if (isset($param['uid']) && intval($param['uid']) > 0)
    	{
    		$result = $this->topicCommentsModel->removeTopicComments($param);
    		UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeTopicComments]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    		return  $result;
    	}
    	
    	// 删除单个评论
		$param['position'] = $param['position']+1;
		$result =   $this->topicCommentsModel->removeTopicComment($param);
		if ($result){
		    AC::$userExpAction['action'] = "deleteTopicCommentByadmin"; //类型
		    AC::$userExpAction['admin'] = TRUE; //管理员操作标记
		    AC::$userExpAction['userid'] =  $param['author_uid']; //被操作用户
		}
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeTopicComments]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }
    
    /**
     * 删除帖子评论
     * @param unknown $param
     * @return boolean
     */
    private function delTopicComment($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	
        if (empty($param['tid']) || empty($param['position']) || empty($param['author_uid']))
		{
			AC::$errorNo    =   'GRC001';
			return  FALSE;
		}
		
		// 返回数据position减1，此处要加上还原
		$param['position'] = $param['position']+1;
		$result =   $this->topicCommentsModel->removeTopicComment($param);
		if ($result){
		    AC::$userExpAction['action'] = "deleteTopicCommentByadmin"; //类型
		    AC::$userExpAction['admin'] = TRUE; //管理员操作标记
		    AC::$userExpAction['userid'] =  $param['author_uid']; //被操作用户
		}
        UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeTopicComment]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }/*}}}*/
    
    /**
     * 编辑圈子
     * @param unknown $param
     * @return boolean|unknown
     */
    private function editGroup($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	 
    	$controller 	=   $this->newController('GroupController');
    	$result         =   $controller->callApi($param);
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][editGroup]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }/*}}}*/
    
    /**
     * 编辑话题
     * @param unknown $param
     * @return boolean|unknown
     */
    private function editTopic($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	 
    	$controller 	=   $this->newController('GroupController');
    	$result         =   $controller->callApi($param);
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][editTopic]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }/*}}}*/
    
    /**
     * 解散圈子
     * @param unknown $param
     * @return boolean|unknown
     */
    private function removeGroup($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    
    	$controller 	=   $this->newController('GroupController');
    	$result         =   $controller->callApi($param);
    	UK::saveLog('manage.log', '['.date("Y-m-d H:i:s",time()).']['.AC::HOST_UID.'][removeGroup]'. DOP::encodeData($param). "[result='.$result.'] \n", TRUE);
    	return  $result;
    }/*}}}*/
    
    // 未发表的话题列表
    private function getUnpublishedTopics($param)/*{{{*/
    {
    	if(!$this->checkAdminUid()){return  FALSE;}
    	$controller 	=   $this->newController('GroupController');
    	$result         =   $controller->callApi($param);
    	
    	return  $result;
    }
    
    /**
     * 推广话题
     * @param unknown $param
     * @return boolean|Ambigous <boolean, Ambigous, StdClass, multitype:number >
     */
    public function promoteTopic($param){
        if(!$this->checkAdminUid()){return  FALSE;}
        
        $tid            =    intval($param['tid']);
        $gid            =    intval($param['gid']);
        $author_uid     =    intval($param['author_uid']);
        if ($tid < 1 || $gid < 1 || $author_uid < 1){
            AC::$errorNo = 'GRC001';
            return FALSE;
        }
        
        $groupController = new GroupController();
        // 从redis中查找圈子信息
        $groupInfo  =   $this->groupInfoModel->groupAddRedis($gid);
        if (empty($groupInfo)){
            AC::$errorNo = 'GRC002';
            return FALSE;
        }
        
        // 获取话题详情
        $topic = $groupController->getTopicInfo(array('tid' => $tid));
        if (empty($topic)){
            return FALSE;
        }

        // 判断数据
        if (empty($groupInfo['mUid']) || $gid != $topic['topic_info']['gid']){
            return FALSE;
        }

        // 数据进入日记
        $groupController->topicIntoDiary($topic, $groupInfo['mUid']);
        if (AC::$errorNo){
            AC::$errorNo = NULL;
        }
        return true;
    }
    
}

